﻿
#region Using directives
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

using Xeno_Race.Graphics;
using Xeno_Race.Helpers;
#endregion

namespace Xeno_Race.Game
{
    /// <summary>
    /// Base game class for all the basic game support.
    /// Connects all helper classes etc.
    /// </summary>
    public class BaseGame : Microsoft.Xna.Framework.Game
    {
        /// <summary>
        /// Graphics
        /// </summary>
        protected GraphicsDeviceManager graphics;

        /// <summary>
        /// Remember graphics device and allow the useage wherever needed via
        /// the Device property.
        /// </summary>
        private GraphicsDevice device;

        /// <summary>
        /// Content
        /// </summary>
        protected ContentManager content;

        /// <summary>
        /// Default resolution of the game.
        /// </summary>
        protected int width = 1024, height = 768; // width = 1440, height = 900;

        /// <summary>
        /// Font for rendering text
        /// </summary>
        protected TextureFont font = null;

        /// <summary>
        /// Create base game
        /// </summary>
        public BaseGame()
        {
            // Basic graphics stuff
            graphics = new GraphicsDeviceManager(this);

            // Limit the framerate to the vertical retrace (VSync on)
            // Enabled because game runs smoother with VSync on! (At least on my machine ;)
            graphics.SynchronizeWithVerticalRetrace = true;

            // Set resolution
            graphics.PreferredBackBufferWidth = width;
            graphics.PreferredBackBufferHeight = height;
            // graphics.ToggleFullScreen();

            // If set to true makes Game runs as fast as possible, 
            // used together with disabling VSync
            this.IsFixedTimeStep = true;

            // Mouse cursor won't be visible
            this.IsMouseVisible = false;
            
            // Basic content stuff
            content = base.Content;
            //content.RootDirectory = "Content";
#if !XBOX360
            // Make sure we use the current directory, not the executing directory,
            // which is not the same for unit tests.
            // Also include the content directory to make the paths easier and shorter
            content.RootDirectory = Directory.GetCurrentDirectory();
#endif
            // And Fps counter
            this.Components.Add(new FpsCounter(this));
        } // BaseGame()


        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            device = graphics.GraphicsDevice;
            // Get resolution in case its different from our desired one
            width = device.Viewport.Width;
            height = device.Viewport.Height;
            // Create font
            font = new TextureFont(graphics.GraphicsDevice, Content, width, height);
            base.Initialize();
        }

        /// <summary>
        /// Load all graphics content
        /// Use this method to make sure a device reset event is handled correctly.
        /// </summary>
        protected override void LoadContent()
        {
            // Nothing yet
            base.LoadContent();
        } // LoadContent()

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            Content.Unload();
            SpriteHelper.Dispose();
        }

        /// <summary>
		/// Draw method of BaseGame
		/// </summary>
		/// <param name="gameTime">Game time</param>
		protected override void Draw(GameTime gameTime)
		{
			// Draw all sprites
			SpriteHelper.DrawSprites(width, height);
            // Write all font to the screen
            font.WriteAll();

			base.Draw(gameTime);
		}

        /// <summary>
        /// Forces drawing of sprites
        /// </summary>
        public void forceSpriteDraw()
        {
            // Draw all sprites
            SpriteHelper.DrawSprites(width, height);
        }

        /// <summary>
        /// Graphics device access for the whole the engine.
        /// </summary>
        /// <returns>Graphics device</returns>
        public GraphicsDevice Device
        {
            get
            {
                return device;
            } // get
        } // Device

        /// <summary>
        /// Content manager access for the whole the engine.
        /// </summary>
        /// <returns>Content manager</returns>
        public new ContentManager Content
        {
            get
            {
                return content;
            } // get
        } // Content

        /// <summary>
        /// Width of our backbuffer we render into.
        /// </summary>
        /// <returns>Int</returns>
        public int Width
        {
            get
            {
                return width;
            } // get
        } // Width

        /// <summary>
        /// Height of our backbuffer we render into.
        /// </summary>
        /// <returns>Int</returns>
        public int Height
        {
            get
            {
                return height;
            } // get
        } // Height

        /// <summary>
        /// BaseGame rectangle
        /// </summary>
        /// <returns>Rectangle</returns>
        public Rectangle ResolutionRect
        {
            get
            {
                return new Rectangle(0, 0, width, height);
            } // get
        } // ResolutionRect
    }
}
